<?php
namespace App\Utilities;

class JiugeForm
{
    /**
     * jiuge form 表单工具类
     * 默认结合laravel5，UI采用amazeUI
     */
    //表单类型：1input-text，2input-file，3input-submit，4input-button，5select，
    //6textarea，7checkbox，8radio，
    private static $types = [
        1=>'text','file','submit','button','select','textarea','checkbox','radio',
    ];
    private static $action;//请求地址

    public function __construct($action='')
    {
        self::$action = $action ? $action : '没有写路由！';
    }

    /**
     * 是否要加 form 外部：action、method、token、、、
     * @param array $formArr：$action、$token、
     * @param array $selArr：表单选项，二维数组
     */
    public static function createForm($selArr=array(),$optionArr=array())
    {
        $selArr = $selArr ? $selArr : array(array(1,'名称','name','',1,array()));
        if (!self::$action) {
            if (!$selArr) { return "没有参数！"; }
            foreach ($selArr as $v) {
                echo self::setElement($v[0],$v[1],$v[2],$v[3],$v[4],$optionArr);
            }
            exit;
        }
        //$formArr = ['action'=>''];
        $html = "<form action='".self::$action."' class='am-form' method='POST' enctype='multipart/form-data'>";
        $html .= csrf_field();
        $html .= "<fieldset>";
        foreach ($selArr as $v) {
            $html = $html . self::setElement($v[0],$v[1],$v[2],$v[3],$v[4],$optionArr);
        }
        $html .= "<button type='button' class='am-btn am-btn-primary' onclick='history.go(-1);'>返回</button> <button type='submit' class='am-btn am-btn-primary'>保存添加</button>";
        $html .= "</fieldset>";
        $html .= "</form>";
        echo $html;
    }

    /**
     * 是否要加 form 外部：action、method、token、、、
     * @param array $formArr：$action、$token、
     * @param array $selArr：表单选项，二维数组
     */
    public static function editForm($selArr=array(),$optionArr=array(),$edit='')
    {
        $selArr = $selArr ? $selArr : array(array(1,'名称','name','',1,array()));
        if (!self::$action) {
            if (!$selArr || !$edit) { return "没有参数！"; }
            foreach ($selArr as $v) {
                echo self::setElement($v[0],$v[1],$v[2],$v[3],$v[4],$v[5],$optionArr,$edit);
            }
            exit;
        }
        //$formArr = ['action'=>''];
        $html = "<form action='".self::$action.'/'.$edit['id']."' class='am-form' data-am-validator method='POST' enctype='multipart/form-data'>";
        $html .= csrf_field();
        $html .= "<input type='hidden' name='_method' value='POST'/>";
        $html .= "<fieldset>";
        foreach ($selArr as $v) {
            $html = $html . self::setElement($v[0],$v[1],$v[2],$v[3],$v[4],$optionArr,$edit);
        }
        $html .= "<button type='button' class='am-btn am-btn-primary' onclick='history.go(-1);'>返回</button> <button type='submit' class='am-btn am-btn-primary'>保存修改</button>";
        $html .= "</fieldset>";
        $html .= "</form>";
        echo $html;
    }

    /**
     * 拼接表单选项：添加
     * @param tinyint $type：
     *          1input-text，2input-file，3input-submit，4input-button，5select，
     *          6textarea，7checkbox，8radio，
     * @param string $nameCN 表单提交的中文属性
     * @param string $name 表单提交的属性
     * @param array $placeholder 表单提示
     * @param array $valite js字段验证
     * @param array $selData 下拉列表选项，二维数组
     * @param array $edit 模型的编辑记录
     * return string
     */
    public static function setElement(
        $type=1,$nameCN='名称',$name='name',
        $placeholder='',$valite=0,$selData=array(),$edit=null)
    {
        if (!$name) { return '表单name参数未填！'; }
        //拼接表单
        $html = "<div class='am-form-group'>";
        $html .= "<label>".$nameCN.' './*ucfirst($name).*/"：</label>";
        if ($type==1) {
            $s = "<input type='".self::$types[$type]."' name='".$name."'";
            $s .= $edit ? " value='".$edit[$name]."'" : "";//编辑页面
            $s .= " placeholder='".$placeholder."'".self::getJsValite($valite);
            $s .= $valite==12 ? " readonly" : "";
            $s .= "/>";
        } else if ($type==2) {
            $s = "<input type='file' name='".$name."' style='padding:5px;width:100%;border:1px solid #d0d0d0;background:#f0f0f0;'/>";
//        } else if (in_array($type,[3,4])) {
//            $s = "<input type='".self::$types[$type]."' class=\"btn btn-success\"/>";
        } else if ($type==5) {
            $s = "<select name='".$name."'".self::getJsValite($valite).">";
            if ($edit && in_array($name,['area','cate','pid'])) {
                $s .= "<option value='0' ";
                $s .= $edit[$name]==0 ? " selected" : "";
                $s .= ">顶级</option>";
            } else if (in_array($name,['area','cate','pid'])) {
                $s .= "<option value='0'>顶级/所有</option>";
            }
            foreach ($selData[$name] as $k=>$v) {
                $s .= "<option value='".$k."'";
                if ($edit) {
                    $s .= $edit[$name]==$k ? " selected" : "";
                }
                $s .= ">".$v."</option>";
            }
            $s .= "</select>";
        } else if ($type==6) {
            $s = "<textarea name='".$name."' placeholder='".$placeholder."'";
            $s .= self::getJsValite($valite)." rows='10'>";
            $s .= $edit ? $edit[$name] : "";
            $s .= "</textarea>";
        } else if ($type==7) {
            foreach ($selData[$name] as $k=>$v) {
                $s = "<label><input type='checkbox' name='".$name."' value='".$k."'/> ".$v."&nbsp;&nbsp;</label>";
            }
        } else if ($type==8) {
            foreach ($selData[$name] as $k=>$v) {
                $s = "<label><input type='radio' name='".$name."' value='".$k."'/> ".$v."&nbsp;&nbsp;</label>";
            }
        } else {
            $s = "";
        }
        $html2 = "</div>";
        return $html.$s.$html2;
    }

    /**
     * js验证
     * @param tinyint $n：1用户名，2手机号，3邮箱，4价格，5数字，6下拉框必选，7网络地址，
     *          8简介，9命名空间，10控制器、方法，11路由，12日期，13文章，
     *          100不做限制，
     * 注：用户名2-20位必填，手机号11位必填，价格保留2位小数，
     */
    public static function getJsValite($n=1)
    {
        if ($n==1) {
            $valite = "minlength='1' maxlength='20' required";
        } else if ($n==2) {
            $valite = "pattern='^1[34578]\\d{9}$' required";
        } else if ($n==3) {
            $valite = "pattern='^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$' required";
        } else if ($n==4) {
            $valite = "pattern='(^[1-9]\\d*(\\.\\d{1,2})?$)|(^0(\\.\\d{1,2})?$)' required";
        } else if ($n==5) {
            $valite = "pattern='^\\d+$' required";
        } else if ($n==6) {
            $valite = "required";
        } else if ($n==7) {
            $valite = "minlength='2' required";
        } else if ($n==8) {
            $valite = "minlength='2' maxlength='255' required";
        } else if ($n==9) {
            $valite = "pattern='^App\\Http\\Controllers\\Admin[0-9a-zA-Z_\\]{0,100}$' required";
        } else if ($n==10) {
            $valite = "pattern='^[0-9a-zA-Z_]{2,50}$' required";
        } else if ($n==11) {
            $valite = "pattern='^/[0-9a-zA-Z_/?=]{2,255}$' required";
        } else if ($n==12) {
            $valite = "class=\"doc-datepicker\" required";
        } else if ($n==13) {
            $valite = "minlength='2' maxlength='2000' required";
        } else {
            $valite = "";
        }
        return ' '.$valite;
    }

    /**
     * 列表展示页面
     */
    public static function getTableList($fieldArr=array(),$dataArr=array())
    {
        if (!$fieldArr) { return "没有列表头！"; }
        $html = "";
        $html .= "<table class=\"am-table am-table-striped am-table-hover table-main\">";
        //拼接表头
        $html .= "<thead><tr>";
        $html .= "<th class=\"table-check\"><input type=\"checkbox\"/></th>";
        foreach ($fieldArr as $v) {
            $html .= "<th class='table-type'>".$v."</th>";
        }
        $html .= "</tr></thead>";
        //拼接表体
        $html .= "<tbody>";
        if ($dataArr) {
            foreach ($dataArr as $data) {
                $html .= "<tr>";
                foreach ($fieldArr as $k=>$v) {
                    $html .= "<td class=\"am-hide-sm-only\"><input type=\"checkbox\" /></td>";
                    $html .= "<td class=\"am-hide-sm-only\">".$data[$k]."</td>";
                }
                $html .= "</tr>";
            }
        } else {
            $html .= "<tr><td class='am-hide-sm-only' colspan='10' style='text-align:center;'>没有记录</td></tr>";
        }
        $html .= "</tbody>";
        $html .= "</table>";
        echo $html;
    }

    /**
     * 详情页面
     */
    public static function show($fieldArr=array(),$dataArr)
    {
        $imgArr = array('thumb','license','img',
            'idcardImg1','idcardImg2','idcardImg3',);
        if (!$fieldArr || !$dataArr) { return "参数错误！"; }
        $html = "";
        $html .= "<table class=\"am-table am-table-striped am-table-hover table-main\">";
        $html .= "<tbody id=\"tbody-alert\">";
        foreach ($fieldArr as $k=>$v) {
            $html .= "<tr><td class=\"am-hide-sm-only\" width='200'>".$v."：</td>";
            $html .= "<td width='500'>";
            if (in_array($k,['detail','intro','param','error','success'])) {
                $html .= "<br/><textarea style='width:100%;height:200px;border:1px solid #e0e0e0;resize:none;outline:none;' readonly>".$dataArr[$k]."</textarea>";
            } /*else if (!in_array($k,$imgArr)) {
                $html .= $dataArr[$k];
            }*/
            if (in_array($k,$imgArr)) {
                $html .= "<br/><img src='".$dataArr[$k]."' width='300'>";
            }
            $html .= "</td></tr>";
        }
        $html .= "</tbody>";
        $html .= "</table>";
        $html .= "<button type=\"button\" class=\"am-btn am-btn-primary\" onclick=\"history.go(-1);\">返回</button>";
        $html .= "<br><br><br>";
        echo $html;
    }
}